package com.eric.rabbit.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author: eric
 * @create: 2022-03-04 18:32 相同key的路由键模式
 * <p>
 * 生产者 -> exchange ->相同的key ->两个不同的队列
 * 根据路由key分发到多个队列分别消费
 */
@Configuration
public class SameKeyDirectRabbitConfig {

    private static final String key = "red";

    @Bean
    public DirectExchange sameKeyDirectExchange() {
        return new DirectExchange("sameKey.direct.exchange");
    }

    @Bean
    public Queue sameKeyDirectQueue1() {
        return new Queue("sameKeyDirectQueue-1", true);
    }

    @Bean
    public Queue sameKeyDirectQueue2() {
        return new Queue("sameKeyDirectQueue-2", true);
    }

    @Bean
    public Binding sameKeyDirectBinding1() {
        return BindingBuilder.bind(sameKeyDirectQueue1()).to(sameKeyDirectExchange()).with(key);
    }

    @Bean
    public Binding sameKeyDirectBinding2() {
        return BindingBuilder.bind(sameKeyDirectQueue2()).to(sameKeyDirectExchange()).with(key);
    }
}
